// pages/todayClient/index.js
const common = require('../../utils/common')
const {
  getDayCustomer
} = require('../../services/customer')

const defaultPage = {
  curPage: 1,
  pageSize: 10,
  noMore: false
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    query: {
      ...defaultPage
    },
    pInfo: {}, //返回的列表数据，里面包含total等
    clientList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu({});
    // 获取数据
    this.onPullDownRefresh();
  },

  // 获取今日订单数
  requestToGetDayCustomer({
    curPage,
    pageSize
  }, pullDownRefresh) {
    let params = {
      curPage: curPage,
      pageSize: pageSize
    }
    getDayCustomer(params).then(res => {
      if (pullDownRefresh) {
        wx.stopPullDownRefresh({});
      }
      this.hanleDayCustomerData(res, curPage, pullDownRefresh);
    }).catch(err => {
      if (pullDownRefresh) {
        wx.stopPullDownRefresh({});
      }
      common.log(err);
    })
  },

  // 处理返回的数据
  hanleDayCustomerData(res, curPage, pullDownRefresh) {
    //设置数据
    var tmpList = this.data.clientList;
    if (pullDownRefresh) {
      tmpList = [];
    }
    //pageInfo
    var tmpPageInfo = this.data.query;
    if (res.page.pages <= curPage) {
      tmpPageInfo.noMore = true;
    } else {
      tmpPageInfo.curPage++;
    }
    //list
    if (res.data.length > 0) {
      tmpList = [...tmpList, ...res.data];
    }
    this.setData({
      query: tmpPageInfo,
      pInfo: res.page,
      clientList: tmpList
    });
  },
  queryChange: function (payload, pullDownRefresh) {
    this.setData({
      query: {
        ...this.data.query,
        ...payload
      }
    });
    this.requestToGetDayCustomer(this.data.query, pullDownRefresh);
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.queryChange(defaultPage, true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.query.noMore) return;
    this.queryChange(this.data.query);
  },
})
